<template>
  <div class="dictionary">
    <div class="header flex-between">
      <div>
        <span class="margin-right-10">查询类型:</span>
        <el-select v-model="selectValue"
                   @change="selectChange"
                   placeholder="请选择">
          <el-option v-for="item in options"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </div>
      <el-button type="primary"
                 @click="dialogVisible = true">新增字典</el-button>
    </div>
    <!-- table -->
    <el-table :data="dictionaryList"
              v-loading="loading"
              style="width: 100%">
      <el-table-column type="index"
                       width="80"
                       label="序号">
      </el-table-column>
      <el-table-column prop="val"
                       label="字典名称">
      </el-table-column>
      <el-table-column prop="remake"
                       label="备注">
      </el-table-column>
      <el-table-column label="操作"
                       width="150">
        <template slot-scope="scope">
          <el-button size="mini"
                     plain
                     type="primary"
                     @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total,prev, pager, next"
                   @current-change="handleCurrentChange"
                   background
                   :total="total">
    </el-pagination>

    <!-- 新增修改字典信息-->
    <el-dialog :title="isEdit?'编辑字典':'新增字典'"
               :before-close="onCancel"
               :close-on-click-modal="false"
               :visible.sync="dialogVisible"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="form"
               label-width="100px">
        <el-form-item label="字典类型"
                      prop="label">
          <el-select v-model="form.label"
                     style="width:100%"
                     placeholder="请选择字典类型">
            <el-option v-for="item in options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="字典名称"
                      prop="val">
          <el-input clearable
                    placeholder="请输入字典名称"
                    v-model="form.val"></el-input>
        </el-form-item>
        <el-form-item label="字典备注"
                      prop="remake">
          <el-input clearable
                    placeholder="请输入字典备注"
                    v-model="form.remake"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="onCancel">取 消</el-button>
        <el-button type="primary"
                   @click="onSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDicList, deleteDic, addDic, updateDic } from '../../services/service_dictionary'

const initForm = () => {
  return {
    label: '',
    remake: '',
    val: ''
  }
}
export default {
  name: 'dictionary',
  data () {
    return {
      loading: false,
      options: [{
        value: 'product_series',
        label: '产品系列'
      }, {
        value: 'shop_type',
        label: '门店类型'
      }, {
        value: 'shop_label',
        label: '门店标签'
      }, {
        value: 'shop_service',
        label: '门店服务'
      },
      {
        value: 'product_special',
        label: '产品特点'
      },
      {
        value: 'goods_taste_name',
        label: '烟头商品口味名称'
      }],
      selectValue: 'product_series',//筛选的绑定值
      // 分页
      current: 1,
      size: 10,
      total: null,
      dictionaryList: [],//字典表单
      // 新增，修改字典弹窗
      dialogVisible: false,
      isEdit: false,
      form: initForm(),
      rules: {
        label: [{ required: true, message: '请选择字典类型', trigger: 'blur' },],
        remake: [{ required: true, message: '请输入字典备注', trigger: 'blur' },],
        val: [{ required: true, message: '请输入字典标题', trigger: 'blur' },]
      }
    }
  },
  methods: {
    // 获取字典列表
    async getAllDicList () {
      const data = {
        current: this.current,
        size: this.size,
        label: this.selectValue,
      }
      try {
        this.loading = true
        const res = await getDicList(data)
        if (res.code == 0) {
          console.log(res);
          this.dictionaryList = res.data.records
          // 按照sort 从小到大排序
          this.dictionaryList.sort((a, b) => {
            return a.sort - b.sort
          })
          this.total = res.data.total
          this.loading = false
        } else {
          this.$message.warning(res.msg)
          this.loading = false
        }
      } catch (error) {
        console.log(error);
        this.loading = false
      }
    },
    // 切换页
    handleCurrentChange (page) {
      this.current = page
      this.getAllDicList()
    },
    // 筛选条件改变
    selectChange () {
      this.getAllDicList()
    },
    // 删除字典
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该字典, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDic(row.id).then(res => {
          if (res.code == 0) {
            this.getAllDicList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            this.$message.warning(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 添加，编辑
    onSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (!this.isEdit) {
            const res = await addDic(this.form)
            if (res.code == 0) {
              this.$message.success('新增成功')
              this.onCancel()
              this.getAllDicList()
            }
          } else {
            const res = await updateDic(this.form)
            if (res.code == 0) {
              this.$message.success('更新成功')
              this.onCancel()
              this.getAllDicList()
            }
          }

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 修改字典
    handleEdit (index, row) {
      const { label, remake, val, id } = row
      this.form = {
        id,
        label,
        remake,
        val
      }
      this.isEdit = true
      this.dialogVisible = true
    },
    // 关闭弹窗
    onCancel () {
      this.dialogVisible = false
      this.isEdit = false
      this.form = initForm()
    }
  },
  created () {
    this.getAllDicList()
  }
}
</script>

<style lang="scss" scoped>
</style>